﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchool.Models.Interfaces;

namespace GameSchool.Models.Repositories
{
    public class QuizRepository 
    {
        GameSchoolDBDataContext db = new GameSchoolDBDataContext();

        //Fetch all quizes 
        public IQueryable<Quiz> GetQuiz()
        {
            var all = from n in db.Quizs
                      orderby n.ID descending
                      select n;

            return all;
        }

        public void AddUserQuiz(UserQuiz q)
        {
            db.UserQuizs.InsertOnSubmit(q);
        }

        public void AddQuiz(Quiz q)
        {                
            db.Quizs.InsertOnSubmit(q);
        }

        public void AddQuestion(Question que)
        {
            db.Questions.InsertOnSubmit(que);
        }

        public void AddChoice(Choice c)
        {
            db.Choices.InsertOnSubmit(c);
        }

        public void AddAnswer(Answer a)
        {
            db.Answers.InsertOnSubmit(a);
        }

        //For fetching questions belonging to a quiz by it's ID
        public IQueryable<Question> GetQuestionsByQuizID(int? qID)
        {
            if (qID == null)
                throw new ArgumentNullException("GetQuestionsByID - null passed as parameter");

            var quest = from questions in db.Questions
                        where questions.QuizID == qID
                        select questions;

            return quest;
        }

        //For fetching all choices belonging to a question by it's ID
        public IQueryable<Choice> GetChoicesByQuestionID(int? questID)
        {
            if (questID == null)
                throw new ArgumentNullException("GetChoiceByID - null passed as parameter");

            var choice = from c in db.Choices
                         where c.QuestionID == questID
                         select c;

            return choice;
        }

        //For fetching all quizzes belonging to a level that is passed in
        public IQueryable<Quiz> GetQuizByLevelID(int? lID)
        {
            if(lID == null)
            throw new ArgumentNullException("GetQuizByLevelID - null passed as parameter"); 
               
            var quizbylevelid = (from quiz in db.Quizs                                  
                                 from levels in db.Levels 
                                 where lID == levels.ID
                                 select quiz);

            return quizbylevelid;
         }
            

         //For fetching a Quiz by it's ID
         public Quiz GetQuizByID(int? qID)
         {
            if(qID == null)
            throw new ArgumentNullException("GetQuizByID - null passed as parameter"); 
                
            var quizbyid = (from quiz in db.Quizs                                   
                            where quiz.ID == qID                             
                            select quiz).SingleOrDefault();

            return quizbyid;
         }

         //For quiz deletion along with all it's data in other tables
         public void DeleteQuiz(int? qID)
         {
             if (qID == null)
                throw new ArgumentNullException("Delete Quiz - null passed as parameter");

             //Delete the quiz reference from Assignments
             var assignment = (from Assignment in db.Assignments
                               where Assignment.ResourceID == qID
                               where Assignment.AssignmentType == "Quiz"
                               select Assignment);

             db.Assignments.DeleteAllOnSubmit(assignment);

             //Marks the quiz from the Quiz table for deletion
             var quiz = (from Quiz in db.Quizs
                         where Quiz.ID == qID
                         select Quiz);

             //Marks the questions from the Questions table for deletion
             var questions = (from Question in db.Questions
                              where Question.QuizID == qID
                              select Question);

             //Each question has choices that need deletion
             foreach (var q in questions)
             {
                 //Find the choices belonging to each question
                 var choices = from Choice in db.Choices
                               where Choice.QuestionID == q.ID
                               select Choice;
                 foreach (var c in choices)
                 {
                     //Find the answer for each choice in the Answers table, not really used anymore though
                     var answers = (from Answer in db.Answers
                                    where Answer.ChoiceID == c.ID
                                    select Answer);

                     db.Answers.DeleteAllOnSubmit(answers); //Delete the answer for each choice

                 }

                 //Delete all the choices belonging to the question
                 db.Choices.DeleteAllOnSubmit(choices);
             }

                //Finally get rid of the questions and the Quiz 
                db.Quizs.DeleteAllOnSubmit(quiz);
                db.Questions.DeleteAllOnSubmit(questions);
          }
                

          public void Save()
          {
                db.SubmitChanges();
          }

        }

}
